package com.bolehui.service.iSys.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bolehui.base.result.Result;
import com.bolehui.domain.dto.OptionDTO;
import com.bolehui.domain.dto.SysDicDTO;
import com.bolehui.domain.po.SysDictData;
import com.bolehui.domain.po.SysDictType;
import com.bolehui.mapper.SysDictTypeMapper;
import com.bolehui.service.iSys.ISysDictTypeService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chendongjie     email:chendongjie@gz.iscas.ac.cn
 * @version 1.0
 * @ClassName SysDictTypeServiceImpl
 * @Description
 * @createTime 2025/2/27-15:24
 * Copyright (C) 2021-2022 CASEEDER, All Rights Reserved.
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {

    @Autowired
    private SysDictTypeMapper dictTypeMapper;

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    public Result getDictDataByType(String dictType){
        List<SysDictData> dictDataByType = dictTypeMapper.getDictDataByType(dictType);
        if(CollectionUtils.isNotEmpty(dictDataByType)){
            SysDicDTO result = new SysDicDTO();
            List<Long> dictCodes = dictDataByType.stream()
                    .map(SysDictData::getDictCode)
                    .collect(Collectors.toList());
            List<String> dictLabels = dictDataByType.stream()
                    .map(SysDictData::getDictLabel)
                    .collect(Collectors.toList());
            result.setDictCodes(dictCodes);
            result.setDictLabels(dictLabels);
            return Result.success(result);
        }
        return Result.fail("");
    }


    public Result getDomain(String dictType){
        List<SysDictData> dictDataByType = dictTypeMapper.getDictDataByType(dictType);
        if(CollectionUtils.isNotEmpty(dictDataByType)){
            ArrayList<OptionDTO> result = new ArrayList<>();
            for (SysDictData sysDictData : dictDataByType) {
                OptionDTO optionDTO = new OptionDTO();
                optionDTO.setValue(sysDictData.getDictCode());
                optionDTO.setLabel(sysDictData.getDictLabel());
                result.add(optionDTO);
            }
            return Result.success(result);
        }
        return Result.fail("");
    }
}
